package workexp.alg.stack;

import workexp.alg.env.ListNode;

public class MinStack {
    private ListNode root = new ListNode();
    private ListNode cur;
    private int min=Integer.MAX_VALUE;
    public MinStack() {
        cur=root;
    }

    public void push(int val) {
        if(min>val){
            min=val;
        }
        ListNode tmp = new ListNode(val);
        tmp.next=cur;
        cur=tmp;
    }

    public void pop() {
        if(cur!=root){
            ListNode tmp = cur;
            cur=cur.next;
            tmp=null;
        }
    }

    public int top() {
        if(cur!=root){
            ListNode tmp = cur;
            cur=cur.next;
            int result = tmp.val;
            tmp=null;
            return result;
        }else{
            throw new RuntimeException();
        }
    }

    public int getMin() {
        return min;
    }
}
